using System;
using System.Xml.Serialization;

namespace SharpMud.Text.Formatting
{
	[XmlRoot("FormatTokenization")]
	public class FormatTokenization
	{
		#region Private Variables
		private System.Collections.Generic.List<Matching.StringMatcher>		_Tokens;
		private SharpMud.DynamicTyping.AssembliedTypeLocation	            _Effect;
        private SharpMud.Text.Formatting.ITextFormatEffect                  _EffectInstance;
		#endregion

		#region Constructors
		public FormatTokenization()
		{
			_Effect = null;
            _Tokens = new System.Collections.Generic.List<SharpMud.Text.Matching.StringMatcher>(); ;
		}
        public FormatTokenization(System.Type effect,SharpMud.Text.Matching.StringMatcher token)
        {
            _Effect = DynamicTyping.AssembliedTypeLocation.FromLoadedType(effect);
            _Tokens = new System.Collections.Generic.List<SharpMud.Text.Matching.StringMatcher>();
            _Tokens.Add(token);
        }
        public FormatTokenization(System.Type effect, params SharpMud.Text.Matching.StringMatcher[] tokens)
        {
            _Effect = DynamicTyping.AssembliedTypeLocation.FromLoadedType(effect);
            _Tokens = new System.Collections.Generic.List<SharpMud.Text.Matching.StringMatcher>(tokens);
        }
        #region Static Construction
        public static FormatTokenization MakeExactTokenization(System.Type effect, string exactToken)
        {
            return new FormatTokenization(effect, SharpMud.Text.Matching.StringMatcher.MakeExactMatcher(exactToken));
        }

        #endregion
        #endregion

        #region Public Properties
        [XmlElement("Token")]
		public SharpMud.Text.Matching.StringMatcher[] Tokens
		{
			get
			{
				return (SharpMud.Text.Matching.StringMatcher[])_Tokens.ToArray();
			}
			set
			{
				_Tokens.AddRange(value);
			}
		}

		[XmlElement("Effect")]
		public SharpMud.DynamicTyping.AssembliedTypeLocation Effect
		{
			get
			{
				return _Effect;
			}
			set
			{
				_Effect = value;
			}
		}
		#endregion

		public ITextFormatEffect GetEffectInstance()
		{
            if (this._EffectInstance == null)
                this._EffectInstance = (ITextFormatEffect)Effect.AcquireTypeInstance();
            return this._EffectInstance;
		}
	}
}
